<template>
	<view>
		<view class="navBar">
			<view class="left">
				<uni-icons type="left" size="24" @click="toBack"></uni-icons>
			</view>
			<view class="center">
				<input type="text" v-model="searchKey" value="" placeholder="输入关键字" @confirm="search" />
				<icon type="search" size="34rpx" color="#1296db"></icon>
			</view>
			<view class="right" @click="search">搜索</view>
		</view>
		<view class="colorBg"></view>
		<view class="goodsList">
			<view class="item" v-for="(item,index) in goods" :key="index" @click="toDetail(item.goodsId)">
				<image :src="item.image" mode="widthFix"></image>
				<view class="goodsName-price">
					<view class="goodsName">{{item.goodsName}}</view>
					<view class="price">
						<text class="yuan">￥</text>
						<text>{{item.price.toFixed(2).split('.')[0]}}.</text>
						<text class="decimal">{{item.price.toFixed(2).split('.')[1]}}</text>
						<text class="sales">已售{{item.sales}}件</text>
					</view>
				</view>
			</view>
		</view>
		<uni-load-more :status="status" :icon-size="16" :content-text="contentText"></uni-load-more>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				searchKey: '',
				pageNum: 1,
				pageSize: 6,
				goods: [],
				totalPage: 0,
				status: 'noMore',
				contentText: {
					contentdown: '上拉加载更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				},
			}
		},
		onReachBottom() {
			if (this.pageNum >= this.totalPage) {
				this.status='noMore';
			} else {
				this.pageNum++;
				this.getData();
			}
		},
		methods: {
			toBack() {
				uni.navigateBack({
					delta: 1
				});
			},
			toDetail(id) {
				uni.navigateTo({
					url: '/pages/detail/detail?id=' + id,
				});
			},
			getData() {
				this.status='loading'
				uni.request({
					url: 'http://localhost:8080/goods/findByKey',
					method: 'GET',
					data: {
						searchKey: this.searchKey,
						pageNum: this.pageNum,
						pageSize: this.pageSize,
					},
					success: res => {
						for (let index in res.data.records) {
							this.goods.push(res.data.records[index]);
						}
						if(this.goods.length<this.pageSize){
							this.status='noMore';
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			getTotalPage() {
				uni.request({
					url: 'http://localhost:8080/goods/findTotalPage',
					method: 'GET',
					data: {
						searchKey: this.searchKey,
						pageNum: this.pageNum,
						pageSize: this.pageSize,
					},
					success: res => {
						this.totalPage = res.data;
						console.log(this.totalPage);
					},
					fail: () => {},
					complete: () => {}
				});
			},
			search() {
				this.goods=[];
				this.pageNum = 1;
				this.getData();
				this.getTotalPage();
			},
		}
	}
</script>

<style lang="scss">
	.navBar {
		display: flex;
		height: 100rpx;
		background-color: white;
		align-items: center;
		padding: 0 30rpx;
		/* #ifdef MP-WEIXIN */
		padding-top: 150rpx;
		/* #endif */

		.left {
			flex: 1;
		}

		.center {
			position: relative;
			flex: 5;
		}

		.right {
			flex: 1;
			padding-left: 20rpx;
			text-align: center;
		}

		input {
			padding: 10rpx 0rpx;
			padding-left: 60rpx;
			border: 1rpx solid #ccc;
			border-radius: 50rpx;
			font-size: 30rpx;
		}

		icon {
			position: absolute;
			left: 20rpx;
			top: 16rpx;
		}
	}

	.colorBg {
		display: block;
		height: 300rpx;
		margin-bottom: -300rpx;
		border-radius: 0 0 50rpx 50rpx;
		background: linear-gradient(to bottom, #1296db, #f7f7f7);
	}

	.goodsList {
		display: flex;
		align-items: center;
		justify-content: space-between;
		flex-wrap: wrap;
		margin: 0 20rpx;

		.item {
			box-sizing: border-box;
			margin-top: 25rpx;
			width: 345rpx;
			height: 500rpx;
			border-radius: 20rpx;
			background-color: white;
			overflow: hidden;

		}

		image {
			width: 100%;
			border-radius: 20rpx 20rpx 0 0;
		}

		.goodsName-price {
			padding: 15rpx 20rpx;

			.goodsName {
				white-space: nowrap;
				text-overflow: ellipsis;
				overflow: hidden;
			}

			.price {
				padding: 20rpx 0;
				font-size: 38rpx;
				font-weight: 700;
				color: red;
			}

			.yuan,
			.decimal {
				font-size: 32rpx;
			}

			.sales {
				color: #646566;
				margin-left: 10rpx;
				font-weight: normal;
				font-size: 28rpx;
			}
		}
	}

	.btm {
		padding: 35rpx;
		text-align: center;
		color: #ccc;
	}
</style>